package com.uchengwang.ui.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.uchengwang.ui.UChengApplication;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by U城网 on 2017/5/17.
 */

public class NetwotkUtil {
    public static final String NET_TYPE_WIFI = "WIFI";
    public static final String NET_TYPE_MOBILE = "MOBILE";
    public static final String NET_TYPE_NO_NETWORK = "no_network";
    public static final String IP_DEFAULT = "0.0.0.0";


    public static boolean isConnectInternet() {
        ConnectivityManager conManager = (ConnectivityManager) UChengApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
        return networkInfo != null?networkInfo.isAvailable():false;
    }

    public static boolean isConnectWifi(Context pContext) {
        ConnectivityManager mConnectivity = (ConnectivityManager)UChengApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = mConnectivity.getActiveNetworkInfo();
        int netType = -1;
        if(info != null) {
            netType = info.getType();
        }

        return netType == 1?info.isConnected():false;
    }

    public static String getNetTypeName(int pNetType) {
        switch(pNetType) {
            case 0:
                return "unknown";
            case 1:
                return "GPRS";
            case 2:
                return "EDGE";
            case 3:
                return "UMTS";
            case 4:
                return "CDMA: Either IS95A or IS95B";
            case 5:
                return "EVDO revision 0";
            case 6:
                return "EVDO revision A";
            case 7:
                return "1xRTT";
            case 8:
                return "HSDPA";
            case 9:
                return "HSUPA";
            case 10:
                return "HSPA";
            case 11:
                return "iDen";
            case 12:
                return "EVDO revision B";
            case 13:
                return "LTE";
            case 14:
                return "eHRPD";
            case 15:
                return "HSPA+";
            default:
                return "unknown";
        }
    }

    public static String getIPAddress() {
        try {
            Enumeration e = NetworkInterface.getNetworkInterfaces();

            while(e.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface)e.nextElement();
                Enumeration inetAddressEnumeration = networkInterface.getInetAddresses();

                while(inetAddressEnumeration.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress)inetAddressEnumeration.nextElement();
                    if(!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }

            return "0.0.0.0";
        } catch (SocketException var4) {
            return "0.0.0.0";
        }
    }

    public String getConnTypeName() {
        ConnectivityManager connectivityManager = (ConnectivityManager)UChengApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo == null?"no_network":networkInfo.getTypeName();
    }

    public static boolean isNetworkAvailable(Context con) {
        ConnectivityManager cm = (ConnectivityManager)con.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm == null) {
            return false;
        } else {
            NetworkInfo netinfo = cm.getActiveNetworkInfo();
            return netinfo == null?false:netinfo.isConnected();
        }
    }
}
